package mortal.game.control
{
    import Message.Public.*;
    import flash.utils.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.scene.layer.utils.*;
    import mortal.game.scene.player.item.*;
    import mortal.game.view.dropItem.*;
    import mortal.game.view.system.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class DropItemController extends Controller
    {
        private var _dropItem:DropItemWindow;
        private var _timer:Timer;
        private var _type:int;

        public function DropItemController()
        {
            
        }

        private function get dropItem() : DropItemWindow
        {
            if (this._dropItem == null)
            {
                this.initView();
            }
            return this._dropItem;
        }

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.AutoPathOpenDropItem, this.onDropItemHandler);
            Dispatcher.addEventListener(EventName.Pick_Item, this.onPickItemHandler);
            Dispatcher.addEventListener(EventName.SpaceDropItem, this.onSpaceDropItemHandler);
            Dispatcher.addEventListener(EventName.AutoPick_Item, this.onAutoPickItemHandler);
            
        }

        private function onAutoPickItemHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as Dictionary;
            var _loc_3:* = _loc_2["entityID"] as SEntityId;
            var _loc_4:* = ThingUtil.dropItemUtil.getDropItem(_loc_3);
            this.dropItem.update(_loc_4.dropItemInfo);
            this.dropItem.show();
            setTimeout(this.updateDropItem, 500, _loc_4.dropItemInfo, _loc_4.removeDropItems(_loc_2));
            
        }

        private function updateDropItem(param1:Object, param2:Array) : void
        {
            this.dropItem.flyItemToBackPack(param2);
            this.dropItem.update(param1);
            this.dropItem.hide();
            
        }

        override protected function initView() : IView
        {
            this._dropItem = new DropItemWindow();
            return this._dropItem;
        }

        private function onSpaceDropItemHandler(event:DataEvent) : void
        {
            if (this.dropItem.isHide)
            {
                ThingUtil.dropItemUtil.getRandomDropItem();
            }
            else
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.Pick_Item, this.dropItem.dropItemInfo.getArray()));
            }
            
        }

        private function onPickItemHandler(event:DataEvent) : void
        {
            var _loc_2:DropItemData = null;
            var _loc_3:Array = null;
            if (event.isResult == false)
            {
                if (event.data is Array)
                {
                    _loc_2 = (event.data as Array)[0] as DropItemData;
                    if (_loc_2)
                    {
                        GameProxy.sceneProxy.pickItem(_loc_2.entityID, event.data as Array);
                    }
                }
                else if (!this.isAutoPick)
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.Pick_Item, this.dropItem.dropItemInfo.getArray()));
                }
            }
            else
            {
                _loc_3 = event.data as Array;
                this.dropItem.show();
                this.dropItem.flyItemToBackPack(_loc_3);
                this.dropItem.update(_loc_3);
                if (this.isAutoPick)
                {
                    this.dropItem.hide();
                }
            }
            
        }

        private function onDropItemHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as DropItemPlayer;
            this.dropItem.update(_loc_2.dropItemInfo);
            this.dropItem.show();
            if (this.isAutoPick)
            {
                GameProxy.sceneProxy.pickItem(_loc_2.dropItemInfo.packet.entityId, _loc_2.dropItemInfo.getArray());
            }
            
        }

        public function get isAutoPick() : Boolean
        {
            return SystemSetter.currentSetter.clickToPick;
        }

    }
}
